from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User
from django.db import transaction
import logging

# 配置日志
logger = logging.getLogger(__name__)

@receiver(post_save, sender=User)
def create_default_user(sender, instance, created, **kwargs):
    """创建默认管理员用户（如果不存在）"""
    if not User.objects.filter(username='admin').exists():
        with transaction.atomic():
            User.objects.create_superuser(
                username='admin',
                email='admin@example.com',
                password='123QWE'
            )
            logger.info("已创建默认管理员用户: admin, 密码: 123QWE")
